package com.zhong.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhong.blog.mapper.UserMapper;
import com.zhong.blog.pojo.User;
import com.zhong.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public int query(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper = wrapper.eq("username",user.getUsername());
        wrapper = wrapper.eq("password",user.getPassword());
//        wrapper.and((queryWrapper)->{
//            queryWrapper.eq("password",user.getPassword());
//        });
        User user1 = userMapper.selectOne(wrapper);
        if (user1 != null){
            return 1;
        }
        return 0;
    }

    @Override
    public User findByName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper = wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public boolean checkLogin(String username, String password) {

        User user = this.findByName(username);
        if (password.equals(user.getPassword())){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User findById(Integer userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public List<User> queryByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",name);
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @Override
    public List<User> queryAll() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public int update(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",user.getId());
        int update = userMapper.update(user, wrapper);
        return update;
    }
}
